package com.cn.appcore.http.error

import android.net.ParseException
import com.blankj.utilcode.util.LogUtils
import com.google.gson.JsonParseException
import com.google.gson.stream.MalformedJsonException
import org.apache.http.conn.ConnectTimeoutException
import org.json.JSONException
import retrofit2.HttpException
import java.net.ConnectException

/**
 *   @auther : Aleyn
 *   time   : 2019/08/12
 */
object ExceptionHandle {

    fun handleException(e: Throwable): ResponseThrowable {
        LogUtils.e(  e.message ?: "none")
        return when (e) {
            is ConnectException,
            is HttpException -> ResponseThrowable(ERROR.NETWORD_ERROR)

            is ConnectTimeoutException,
            is java.net.UnknownHostException,
            is java.net.SocketTimeoutException -> ResponseThrowable(ERROR.TIMEOUT_ERROR)

            is JsonParseException,
            is JSONException,
            is ParseException,
            is MalformedJsonException -> ResponseThrowable(ERROR.PARSE_ERROR)
            is javax.net.ssl.SSLException -> ResponseThrowable(ERROR.SSL_ERROR)
            else -> ResponseThrowable(ERROR.UNKNOWN)
        }
    }
}